﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RMS.Models.Repository;
using RMS.Models;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Collections;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace RMS.Pages
{
    public partial class Reports : System.Web.UI.Page {

        private Repository repo = new Repository();

        protected void Page_Load(object sender, EventArgs e) {
                        
            if (!IsPostBack) {
       
                // Setup Report Selection Drop Down
                List<ListItem> items = new List<ListItem>();
                items.Add(new ListItem("# of Retreatants", "# of Retreatants"));
                items.Add(new ListItem("Retreat Lengths By Day", "Retreat Lengths By Day"));
                items.Add(new ListItem("Total Revenue", "Total Revenue"));
                items.Add(new ListItem("Volunteer Submissions", "Volunteer Submissions"));
                ReportDropDown.Items.AddRange(items.ToArray());

                // Setup Sort Drop Down
                List<ListItem> items2 = new List<ListItem>();
                items2.Add(new ListItem("Ascending", "Ascending"));
                items2.Add(new ListItem("Descending", "Descending"));
                SortOrderDropDown.Items.AddRange(items2.ToArray());
                                        
            }
                       
            // Hide on Page Load
            GridView2.Visible = false;
            btnExportReport.Visible = false;
        }
        
        // Button click even populates GridView based on report drop down menu selection
        protected void btnGenerateReport_Click(object sender, EventArgs e) {
           
            // Clear GridView
            GridView2.DataSource = null;
            GridView2.DataBind();
            GridView2.Columns.Clear();
                        
            // Generate Report based on user drop down selection.
            if (ReportDropDown.SelectedValue.Equals("# of Retreatants")) {
                generateRetreatCountReport();
            }
            else if (ReportDropDown.SelectedValue.Equals("Retreat Lengths By Day"))
            {
                generateRetreatLengthReport();
            }
            else if (ReportDropDown.SelectedValue.Equals("Total Revenue")) {
                generateRetreatRevenueReport();
            }
            else if (ReportDropDown.SelectedValue.Equals("Volunteer Submissions"))
            {
                generateVolunteerSubmissionReport();
            }

        }
        
        // Method generates volunteer submission report
        protected void generateVolunteerSubmissionReport()
        {
            var volunteerItems = repo.VolunteerItems;
            List<ListItem> rItems = new List<ListItem>(); // report items (or recrods)

            // Loop cycles through Volunteer Items
            foreach (VolunteerItem v in volunteerItems)
            {
                string theName = getFullName(v.User_Us_ID);
                rItems.Add(new ListItem(theName, v.VolunteerItemType));
            }
            
            // Customize Report Fields
            BoundField nameField = new BoundField();
            nameField.HeaderText = "Volunteer Name";
            nameField.DataField = "text";
            BoundField volunteerTypeField = new BoundField();
            volunteerTypeField.HeaderText = "Volunteering Type";
            volunteerTypeField.DataField = "value";
            volunteerTypeField.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
            GridView2.Columns.Add(nameField);
            GridView2.Columns.Add(volunteerTypeField);

            // Sorting the List
            var sortedList = rItems.OrderBy(item => item.Text).ToList();

            if (SortOrderDropDown.SelectedValue.Equals("Ascending"))
            {
                sortedList = rItems.OrderBy(item => item.Text).ToList();
            }
            else if (SortOrderDropDown.SelectedValue.Equals("Descending"))
            {
                sortedList = rItems.OrderByDescending(item => item.Text).ToList();
            }

            // Setup/Bind GridView
            GridView2.DataSource = sortedList;
            GridView2.DataBind();

            // Make GridView Visible
            GridView2.Visible = true;
            btnExportReport.Visible = true;

        }

        // method searchs for user based on user id, returns full name string
        protected string getFullName(int userID)
        {
            string name = "";
            foreach (User u in repo.Users)
            {
                if (u.Us_ID == userID)
                {
                    name += u.Us_FName + " " + u.Us_LName;
                    break;
                }
            }

            return name;
        }

        // Method generates retreatant count report
        protected void generateRetreatCountReport() {
            var retreatItems = repo.Retreats;
            List<ListItem> rItems = new List<ListItem>();

            // Loop cycles through retreats
            foreach (Retreat r in retreatItems)
            {
                int curCount = countRetreatants(r.Ret_ID);
                string myString = curCount.ToString();

                rItems.Add(new ListItem(r.Ret_Title, myString));
            }

            // Customize Report Fields
            BoundField retreatField = new BoundField();
            retreatField.HeaderText = "Retreat Title";
            retreatField.DataField = "text";
            BoundField countField = new BoundField();
            countField.HeaderText = "Number of Retreatants";
            countField.DataField = "value";
            countField.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
            GridView2.Columns.Add(retreatField);
            GridView2.Columns.Add(countField);
            
            // Sorting the List
            var sortedList = rItems.OrderBy(item => item.Text).ToList();

            if (SortOrderDropDown.SelectedValue.Equals("Ascending")) {
                sortedList = rItems.OrderBy(item => item.Text).ToList();
            }
            else if (SortOrderDropDown.SelectedValue.Equals("Descending")) {
                sortedList = rItems.OrderByDescending(item => item.Text).ToList();
            }
            
            // Setup/Bind GridView
            GridView2.DataSource = sortedList;
            GridView2.DataBind();

            // Make GridView Visible
            GridView2.Visible = true;
            btnExportReport.Visible = true;
            
        }

        // Method generates retreat total revenue report
        protected void generateRetreatRevenueReport() {
            var retreatItems = repo.Retreats;
            List<ListItem> rItems = new List<ListItem>();

            // Loop cycles through retreats
            foreach (Retreat r in retreatItems) {
                decimal retRevenue = sumRevenue(r.Ret_ID);
                string myString = retRevenue.ToString();

                rItems.Add(new ListItem(r.Ret_Title, myString));
            }

            // Customize Report Fields
            BoundField retreatField = new BoundField();
            retreatField.HeaderText = "Retreat Title";
            retreatField.DataField = "text";
            BoundField countField = new BoundField();
            countField.HeaderText = "Retreat Revenue Total";
            countField.DataField = "value";
            countField.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
            countField.DataFormatString = "${0:C}";

            // Sorting the List
            var sortedList = rItems.OrderBy(item => item.Text).ToList();
            if (SortOrderDropDown.SelectedValue.Equals("Ascending"))
            {
                sortedList = rItems.OrderBy(item => item.Text).ToList();
            }
            else if (SortOrderDropDown.SelectedValue.Equals("Descending"))
            {
                sortedList = rItems.OrderByDescending(item => item.Text).ToList();
            }
            
            // Add formatted fields to GridView
            GridView2.Columns.Add(retreatField);
            GridView2.Columns.Add(countField);
                        
            // Setup/Bind GridView
            GridView2.DataSource = sortedList;
            GridView2.DataBind();
                                    
            // Make GridView Visible
            GridView2.Visible = true;
            btnExportReport.Visible = true;
        }
        
        // Method generates retreat length report
        protected void generateRetreatLengthReport() {
            var retreatItems = repo.Retreats;
            List<ListItem> rItems = new List<ListItem>();

            // Loop cycles through retreats
            foreach (Retreat r in retreatItems) {
               
                rItems.Add(new ListItem(r.Ret_Title, r.Ret_Length));
            }

            // Customize Report Fields
            BoundField retreatField = new BoundField();
            retreatField.HeaderText = "Retreat Title";
            retreatField.DataField = "text";
            BoundField countField = new BoundField();
            countField.HeaderText = "Retreat Length By Day";
            countField.DataField = "value";
            countField.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
            GridView2.Columns.Add(retreatField);
            GridView2.Columns.Add(countField);

            // Sorting the List
            var sortedList = rItems.OrderBy(item => item.Text).ToList();
            if (SortOrderDropDown.SelectedValue.Equals("Ascending"))
            {
                sortedList = rItems.OrderBy(item => item.Text).ToList();
            }
            else if (SortOrderDropDown.SelectedValue.Equals("Descending"))
            {
                sortedList = rItems.OrderByDescending(item => item.Text).ToList();
            }
            
            // Setup/Bind GridView
            GridView2.DataSource = sortedList;
            GridView2.DataBind();

            // Make GridView Visible
            GridView2.Visible = true;
            btnExportReport.Visible = true;
        }

        // Method counts num of retreatants based on retreat ID parameter
        protected int countRetreatants(int retreatID) {
            int count = 0;
            var reservationItems = repo.Reservations;
                      
            
            foreach (Reservation d in reservationItems) {
                foreach (OrderLine c in d.OrderLines)
                {
                    if (c.Retreat.Ret_ID == retreatID) {
                        count++;
                    }
                }
            }
            
            return count;
        }

        // Method sums retreat revenue based on retreat ID parameter
        protected decimal sumRevenue(int retreatID) {
            decimal revenue = 0;
            var reservationItems = repo.Reservations;
            
            foreach (Reservation d in reservationItems) {
                foreach (OrderLine c in d.OrderLines)
                {
                    if (c.Retreat.Ret_ID == retreatID)
                    {
                        revenue += c.Retreat.Ret_Cost;
                    }
                }
            }
            
            return revenue;
        }

        // Method exports currently generated report to Microsoft Excel
        protected void btnExportReport_Click(object sender, System.EventArgs e) {
            GridViewExportUtil.Export("Report.xls", this.GridView2);
        }

        protected void ReportDropDown_SelectedIndexChanged(object sender, EventArgs e) {
            // Clear GridView
            GridView2.DataSource = null;
            GridView2.DataBind();
            GridView2.Columns.Clear();
                        
            if (ReportDropDown.SelectedValue.Equals("# of Retreatants"))
            {
                //generateRetreatCountReport();
            }
            else if (ReportDropDown.SelectedValue.Equals("Retreat Lengths By Day"))
            {
                //generateRetreatLengthReport();
            }
            else if (ReportDropDown.SelectedValue.Equals("Total Revenue"))
            {
                //generateRetreatRevenueReport();
            }
       
        }

    }
}